package com.uchedao.seller.ui.carlist.m_service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.uchedao.seller.ui.carlist.db.CarDbHelper;
import com.uchedao.seller.ui.carlist.entity.CityEntity;
import com.uchedao.seller.ui.carlist.entity.ProvinceEntity;
import com.uchedao.seller.ui.carlist.response.MyCityResponse;
import com.uchedao.seller.ui.carlist.response.MyProvinceResponse;

import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by ZhangZhanghua on 2015/10/28 0028.
 */
public class DownloadLocationService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private CarDbHelper carDbHelper;
    private static ArrayList<ProvinceEntity> provinceList;
    private static ArrayList<CityEntity> cityList;

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
       final RequestQueue queue = Volley.newRequestQueue(this);
        String url3 = "http://testopenapi.uchedao.com/base/location/province";
       final String url4 = "http://testopenapi.uchedao.com/base/location/city?province_id=";
        carDbHelper = CarDbHelper.getInstance(this);
        carDbHelper.beginTransaction();
        carDbHelper.deleteAllTable();
        JsonRequest proRequest = new JsonObjectRequest(url3, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.e("pro", response + "");
                MyProvinceResponse provinceResponse = new Gson().fromJson(response.toString(), MyProvinceResponse.class);
                provinceList = (ArrayList<ProvinceEntity>) provinceResponse.getData().getList();
                for (ProvinceEntity province : provinceList) {
                    carDbHelper.insertProvince(province.getId(), province.getName(), province.getFirst_char(), province.getFull_spell());
                }

                for (int i=0;i<provinceList.size();i++){
                    JsonRequest cityRequest = new JsonObjectRequest(url4+provinceList.get(i).getId(), new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            Log.e("city", response + "");
                            MyCityResponse cityResponse = new Gson().fromJson(response.toString(), MyCityResponse.class);
                            cityList = (ArrayList<CityEntity>) cityResponse.getData().getList();
                            for (CityEntity city : cityList) {
                                carDbHelper.insertCity(city.getId(), city.getProvince_id(), city.getName(), city.getFirst_char(), city.getFull_spell());
                            }
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    });
                    queue.add(cityRequest);
                    if (i==provinceList.size()){
                        carDbHelper.setTransactionSuccessful();
                        carDbHelper.endTransaction();
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(proRequest);

    }
}

